(function () {
	let params = dealUrl(location.href);

	$("nav a:eq(2)")
		.html(params.title)
		.attr(
			"href",
			`../pages/comparison.html?title=${params.title}&categoryid=${params.categoryid}`
		);

	$.ajax({
		url: `http://chst.vip:1234/api/getproductlist?categoryid=${params.categoryid}&pageid=1`,
	}).then((res) => {
		let total = Math.ceil(res.totalCount / res.pagesize);
		let html = "";
		for (var i = 1; i <= total; i++) {
			html += `<option value="${i}">${i}/${total}</option>`;
		}
		$(".changePage select").append(html);
		$('.prev').attr('disabled','disabled')
		render(res.result);

		$(".changePage select").change(function () {
			let n=$(this).val() * 1
			console.log(n)
			$.ajax({
				url: `http://chst.vip:1234/api/getproductlist?categoryid=${
					params.categoryid
				}&pageid=${n}`,
			}).then((res) => {
				render(res.result);
			});
			isDisable(n, total);
		});

		$(".next").click(function () {
			let n = $("select").val() * 1 + 1;
			$("select option")
				.eq(n - 1)
				.prop("selected",'selected')
				.siblings()
				.removeProp("selected");
			$.ajax({
				url: `http://chst.vip:1234/api/getproductlist?categoryid=${params.categoryid}&pageid=${n}`,
			}).then((res) => {
				render(res.result);
			});
			isDisable(n, total);
		});

		$(".prev").click(function () {
			let n = $("select").val() * 1 - 1;
			$("select option")
				.eq(n - 1)
				.prop("selected", "selected")
				.siblings()
				.removeProp("selected");
			$.ajax({
				url: `http://chst.vip:1234/api/getproductlist?categoryid=${params.categoryid}&pageid=${n}`,
				
			}).then((res) => {
				render(res.result);
			});
			isDisable(n, total);
		});
	});

	function isDisable(num, total) {
		if (num === 1) {
			$(".prev").attr("disabled", "disabled");
			$(".next").removeAttr("disabled");

		} else if (num === total) {
			$(".next").attr("disabled", "disabled");
			$(".prev").removeAttr("disabled");
		} else {
			$(".prev").removeAttr("disabled");
			$(".next").removeAttr("disabled");

		}
	}
	function render(res) {
		let str = res.reduce((value, item) => {
			return (
				value +
				`
				<a href="../pages/productList.html?categoryid=${item.categoryId}&productid=${item.productId}&title=${params.title}">
				<li>
                ${item.productImg}
                <div class="fr">
                 <h4>${item.productName}</h4>
                 <p class="price">${item.productPrice}</p>
                 <p class="other">
                 <span class="price-num">${item.productQuote}</span>
                 <em>
                     <i class="iconfont">&#xe602;</i>
                     <span class="comments">${item.productCom}</span>
                 </em>
            </p>
        </div>
    </li>
				</a>
            
				`
			);
		}, "");
		$(".info").html(str);
	}
})();
